<?php
/**
 * 文章栏目管理
 *
 * PHP Version 5.6
 *
 * @author    guanxu <510104561@qq.com>
 * @time      2017/08/11
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;
use Think\Page;
use Common\Common\UploadUtil;
Class ColumnController extends CommonController {

    /**
     * 栏目添加页面
     * @return mixed
     */
    public function addShow() {
        $column_id = I("get.id");
        session("column_id",$column_id);
        $rank = M("wzjs_develop_column_relation")->where(["column_id"=>$column_id])->getField("rank");
        $this->assign([
            "column_id" => $column_id ,
            "rank" => $rank,
        ]);
        $this->display("column/add");
    }

    /**
     * 栏目添加
     */
    public function add() {
        $post = I("");
        $result = D("Column")->add($post);
        if($result) $this->success("添加成功!",U('Article/index'));
        $this->error("添加失败!");
    }

    /**
     * 栏目首页
     * @return mixed
     */
    public function index() {
        $result = M("wzjs_develop_column")
            ->where(["wzjs_develop_column.status" =>ConstantUtil::GLOBAL_TRUE ])
            ->join("left join wzjs_develop_column_relation on wzjs_develop_column_relation.column_id = wzjs_develop_column.id")
            ->field("
                wzjs_develop_column.* ,
                wzjs_develop_column_relation.column_id ,
                wzjs_develop_column_relation.column_parent,
                wzjs_develop_column_relation.rank,
                wzjs_develop_column_relation.number
            ")
            ->select();
        $column_id = session("column_id");
        if(!empty($column_id)) {
            $number = M("wzjs_develop_column_relation")->where(["column_id"=>$column_id])->getField("number");
            $number_array = [];
            for($i=0;$i<strlen($number)/4;$i++) {
                $number_array[] = intval(substr($number,$i*4,4));
            }
            foreach($result as $key => $val) {
                if(in_array($val["column_id"],$number_array)) $result[$key]["column_open"] = 1;
            }
        }
        $result = json_encode($result);
        echo $result;
    }

    /**
     * 编辑显示
     */
    public function editorShow() {
        $column_id = I("get.id");
        session("column_id",$column_id);
        $list = M("wzjs_develop_column")
            ->where(["wzjs_develop_column.id"=>$column_id])
            ->join("wzjs_develop_column_relation on wzjs_develop_column.id = wzjs_develop_column_relation.column_id")
            ->find();
        $this->assign([
            "column_id" => $column_id ,
            "list" => $list,
        ]);
        $this->display("column/editor");
    }


    /**
     * 获取正确CODE
     * @param $parent_id
     * @return mixed|string
     */
    public function parentCode($parent_id) {
        if(empty($parent_id)) return "";
        $code = "";
        $parent = M("wzjs_develop_column_relation")->where(["column_id"=>$parent_id])->find();
        if($parent["id"] != 1) {
            if($parent["rank"] > 1) {
                $parent_id = substr($parent["number"],4,4);
                $parent_id = preg_replace('/^0*/', '', $parent_id);
            }
            $code = M("wzjs_develop_column")->where(["id"=>$parent_id])->getField("code");
            $code.="/";
        }
        return $code;
    }
    /**
     * 编辑
     * @return bool
     */
    public function editor() {
        $column_id = I("column_id");
        if(empty($column_id))  $this->error("参数有误!");
        $column["name"]     = I("post.name");
        $column["sort"]     = I("post.sort") ?:1;
        $column["content"]  = I("post.editorValue");
        $column["logo"]     = I("post.logo");
        $column["show"]     = I("post.show");
        $column["remark"]   = I("post.remark");
        $parent_id = M("wzjs_develop_column_relation")->where(["column_id"=>$column_id])->find("column_parent");
        if(!empty($_FILES)) {
            $code = $this->parentCode($parent_id);
            $uploadImg = A("Upload")->image("","","",$code);
            if(empty($uploadImg)) goto GoOn;
            foreach($uploadImg as $key=>$val) {
                $column["title_img"] = "/".UploadUtil::UPLOAD_PATH."/".$code."".$val["savepath"].$val["savename"];
            }
        }
        GoOn:
        M("wzjs_develop_column")->where(["id"=>$column_id])->save($column);
        $this->success("修改成功!",U('Column/editorShow?id='.$column_id.''));
        End:
        $this->error("修改失败!");
    }

    /**
     * 删除栏目 【物理删除】
     */
    public function del() {
        $column_id      = I("get.id");
        $column_parent = $this->delBool($column_id);
        $this->success("删除成功",U('Article/index?id='.$column_parent.''));
    }


    public function delBool($column_id) {
        if(empty($column_id)) return false;
        $column         = M("wzjs_develop_column_relation")->where(["column_id"=>$column_id])->field("column_parent","number")->find();
        $column_parent  = $column["column_parent"];
        $number         = $column["number"];
        $column_id_del  =  M("wzjs_develop_column_relation")->where(["number"=>["like","".$number."%"]])->getField("column_id",true);
        // 删除关系栏目
        if(empty($column_id_del)) $this->error("删除失败!");
        M("wzjs_develop_column")->where(["id"=>["IN",$column_id_del]])->delete();
        // 删除关系
        M("wzjs_develop_column_relation")->where(["number"=>["like","".$number."%"]])->delete();
        return $column_parent;
    }

    /**
     * 栏目详情
     * @return bool|string
     */
    public function info() {
        $column_id = session("column_id");
        if(empty($column_id)) return false;
        $number = M("wzjs_develop_column_relation")->where(["column_id"=>$column_id])->getField("number");
        for($i=0;$i<strlen($number)/4;$i++) {
            $self_column    = substr($number,4*$i,4);
            $column[]       = $self_column;
        }
        if(empty($column)) return '';
        $list = M("wzjs_develop_column")->where(["id"=>["IN",$column]])->field("name")->select();
        echo  json_encode($list);
    }

    public function rank2List() {
        $list = M("wzjs_develop_column_relation")
            ->where(["rank"=>2])
            ->join("left join wzjs_develop_column on wzjs_develop_column.id = wzjs_develop_column_relation.column_id and wzjs_develop_column.status = 1")
            ->field("wzjs_develop_column.id as id , wzjs_develop_column.name as name")
            ->select();
        return json_encode($list);
    }
    public function sonList() {
        $column_id = I("id");
        if(empty($column_id)) return "";
        $list = M("wzjs_develop_column_relation")
            ->where(["column_parent"=>$column_id])
            ->join("left join wzjs_develop_column on wzjs_develop_column.id = wzjs_develop_column_relation.column_id and wzjs_develop_column.status = 1")
            ->field("wzjs_develop_column.id as id , wzjs_develop_column.name as name")
            ->select();
        if(empty($list)) return "";
        return json_encode($list);
    }


    public function numberToArticle() {
        $number = I("number");
        $number = explode(",",$number);
        $num="0001";
        foreach($number as $val) {
            if(empty($val)) continue;
            $num.= sprintf("%04d", $val);
        }
        $column_id = M("wzjs_develop_column_relation")->where(["number"=>$num])->getField("column_id");
        $column_name = M("wzjs_develop_column")->where(["id"=>$column_id])->getField("name");
        $article = M("wzjs_develop_article")->where(["column_id"=>$column_id])->select();
        if(empty($article)) return "";
        return json_encode(["body"=>$article,"column_name"=>$column_name]);
    }
}